<?php
/*
 * Sips Service Class
 *
 * This class contains specific Sips related code
 */
class RM_Sips_Service {

    var $_ipnLogFile;

    var $_paramFile;

    /**
     * Public constructor
     */
    public function  __construct() {
        $this->_ipnLogFile = RM_Environment::getConnector()->getCorePath().DIRECTORY_SEPARATOR.'userdata'.DIRECTORY_SEPARATOR.'logs'.DIRECTORY_SEPARATOR.'sips_ipn_log.txt';
        $this->_paramFile = RM_Environment::getConnector()->getCorePath().DIRECTORY_SEPARATOR.'userdata'.DIRECTORY_SEPARATOR.'temp'.DIRECTORY_SEPARATOR."sips";
    }

    public function processResponse(){

        // get the settings
        $configObj = new RM_SipsConfig();
        $config = $configObj->getSettings()->current();

	
        $fp=fopen($this->_ipnLogFile, "a");
        fwrite( $fp, "Logging all server reponses.\n");
	

	$DATA = $_POST['DATA'];
	// Invalidate data if it is in the wrong format
	if (!preg_match(':^[a-zA-Z0-9]+$:', $DATA))
	$DATA = '';
	$message="message=$DATA";

	$path_bin =  $config->cgibin_path."response";
        $parm.= $this->_paramFile;
        $message .= " ".$parm;

	// Appel du binaire response
	$result=exec("$path_bin $message");
	$table = explode ("!", $result);
	$code = $table[1];
	$error = $table[2];
	$merchant_id = $table[3];
	$merchant_country = $table[4];
	$amount = $table[5];
	$transaction_id = $table[6];
	$payment_means = $table[7];
	$transmission_date= $table[8];
	$payment_time = $table[9];
	$payment_date = $table[10];
	$response_code = $table[11];
	$payment_certificate = $table[12];
	$authorisation_id = $table[13];
	$currency_code = $table[14];
	$card_number = $table[15];
	$cvv_flag = $table[16];
	$cvv_response_code = $table[17];
	$bank_response_code = $table[18];
	$complementary_code = $table[19];
	$i=20;
	if ( $config->version === 'WINDOWS_600' or $config->version === 'LINUX_600' ) {
		$complementary_info = $table[$i++];
	}
	$ps_vendor_id = $return_context = $table[$i++];
	$caddie = $table[$i++];
	$receipt_complement = $table[$i++];
	$merchant_language = $table[$i++];
	$language = $table[$i++];
	$customer_id = $table[$i++];
	$order_id = $table[$i++];
	$customer_email = $table[$i++];
	$customer_ip_address = $table[$i++];
	$capture_day = $table[$i++];
	$capture_mode = $table[$i++];
	$data = $table[$i];

	
	if (( $code == "" ) && ( $error == "" ) ){
            fwrite($fp, "Configuration with the payment method.".$path_bin);
	}
	
	else if ( $code != 0 ){
            fwrite ($fp, "Error executing the payment procedure<br/>Error: ".$error);
	} else {
		
            fwrite( $fp, "merchant_id : $merchant_id\n");
            fwrite( $fp, "merchant_country : $merchant_country\n");
            fwrite( $fp, "amount : $amount\n");
            fwrite( $fp, "transaction_id : $transaction_id\n");
            fwrite( $fp, "transmission_date: $transmission_date\n");
            fwrite( $fp, "payment_means: $payment_means\n");
            fwrite( $fp, "payment_time : $payment_time\n");
            fwrite( $fp, "payment_date : $payment_date\n");
            fwrite( $fp, "response_code : $response_code\n");
            fwrite( $fp, "payment_certificate : $payment_certificate\n");
            fwrite( $fp, "authorisation_id : $authorisation_id\n");
            fwrite( $fp, "currency_code : $currency_code\n");
            fwrite( $fp, "card_number : $card_number\n");
            fwrite( $fp, "cvv_flag: $cvv_flag\n");
            fwrite( $fp, "cvv_response_code: $cvv_response_code\n");
            fwrite( $fp, "bank_response_code: $bank_response_code\n");
            fwrite( $fp, "complementary_code: $complementary_code\n");
            fwrite( $fp, "return_context: $return_context\n");
            fwrite( $fp, "caddie : $caddie\n");
            fwrite( $fp, "receipt_complement: $receipt_complement\n");
            fwrite( $fp, "merchant_language: $merchant_language\n");
            fwrite( $fp, "language: $language\n");
            fwrite( $fp, "customer_id: $customer_id\n");
            fwrite( $fp, "order_id: $order_id\n");
            fwrite( $fp, "customer_email: $customer_email\n");
            fwrite( $fp, "customer_ip_address: $customer_ip_address\n");
            fwrite( $fp, "capture_day: $capture_day\n");
            fwrite( $fp, "capture_mode: $capture_mode\n");
            fwrite( $fp, "data: $data\n");


            $d['order_id'] = $order_id;

            if($response_code == 0  ) {
                    $d['order_status'] = $config->verified_status;
            }
            else {

                    $d['order_status'] = $config->invalid_status;
                    $d['order_comment'] = str_replace("<br />","\n",addslashes(RM_Environment::getInstance()->getRouter()->_('User.Sips.Main','PaymentFailed')." ".$sips_response_code[$response_code]) );
                    if ($response_code==5) {
                            $d['order_comment'] .= "\n".str_replace("<br />","\n",RM_Environment::getInstance()->getRouter()->_('User.Sips.Main','FailedReason')." ".$sips_bank_cb_response_code[$bank_response_code] );
                    }
            }

            $d['order_comment'] .= "\n".addslashes(RM_Environment::getInstance()->getRouter()->_('User.Sips.Main', 'PaymentMeans')).$payment_means ;
            $d['order_comment'] .= "\n".addslashes(RM_Environment::getInstance()->getRouter()->_('User.Sips.Main','TransmissionDate')).$transmission_date;
            $d['order_comment'] .= "\n".addslashes(RM_Environment::getInstance()->getRouter()->_('User.Sips.Main','PaymentTime')).$payment_time ;
            $d['order_comment'] .= "\n".addslashes(RM_Environment::getInstance()->getRouter()->_('User.Sips.Main','PaymentDate')).$payment_date ;
            $d['order_comment'] .= "\n".addslashes(RM_Environment::getInstance()->getRouter()->_('User.Sips.Main','Certificate')).$payment_certificate ;
            $d['order_comment'] .= "\n".addslashes(RM_Environment::getInstance()->getRouter()->_('User.Sips.Main','AuthorisationID')).$authorisation_id ;


            fwrite( $fp, "-------------------------------------------\n");
            fclose ($fp);

            return $d;
	}
	
        fwrite( $fp, "-------------------------------------------\n");
        fclose ($fp);
    }

    public function checkoutCCForm($total, $reservation_id, $customer){

        // get the settings
        $configObj = new RM_SipsConfig();
        $config = $configObj->getSettings()->current();

        $parm="merchant_id=".$config->merchant_id;
	$parm .=" merchant_country=".$config->country;
	$parm.=" amount=".$total;
	$parm.=" currency_code=".$config->currency_id;
	$parm.=" ".$this->_paramFile;
	$parm.=" transaction_id=".$reservation_id;

	$parm.=" normal_return_url=".RM_Environment::getInstance()->getRouter()->_('Sips', 'process', array("page" => "process.phtml"));
	$parm.=" cancel_return_url=".RM_Environment::getInstance()->getRouter()->_('Sips', 'cancel', array("page" => "cancel.phtml"));
	$parm.=" automatic_response_url=".RM_Environment::getInstance()->getRouter()->_('Sips', 'process', array("page" => "process.phtml"));

	$parm.=" language=";
	$parm.=" en_GB";
	$parm.=" payment_means=".$config->payment_means;

	$parm.=" header_flag=".$config->header_flag;
	$parm.=" capture_day=".$config->capture_day;
	$parm.=" capture_mode=".$config->capture_mode;
	
	$parm.=" customer_id=".$customer['id'];
	$parm.=" customer_email=".$customer['email'];

	//$parm.=" return_context=".$ps_vendor_id;

	$parm.=" order_id=".$reservation_id;

	$path_bin = $config->cgibin_path."request";

	if ( $config->version === 'LINUX_500' or $config->version === 'LINUX_600' ) {
		$parm=escapeshellcmd($parm);
	}
	$result=exec("$path_bin $parm");
	$table = explode ("!", "$result");
	$code = $table[1];
	$error = $table[2];
	$message = $table[3];


  	if (( $code == "" ) && ( $error == "" ) ){
		echo RM_Environment::getInstance()->getRouter()->_('User.Sips.Main','ExecReqNotFound')." ".$config->cgibin_path.RM_Environment::getInstance()->getRouter()->_('User.Sips.Main','ExecReqFile')." ".$path_bin;
 	}

	else if ($code != 0){
            echo RM_Environment::getInstance()->getRouter()->_('User.Sips.Main','RequestError')." ".$error;
	} else {
            echo ("  $message ");
	}
    }
}